package com.example.aiyou.passwordmanage.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.aiyou.passwordmanage.R;
import com.example.aiyou.passwordmanage.pojo.DetailView;
import com.example.aiyou.passwordmanage.pojo.Password;

import java.util.List;

/**
 * Created by hello on 2017-09-08.
 */

public class DetailAdpater extends RecyclerView.Adapter<DetailAdpater.ViewHolder> implements View.OnClickListener,View.OnLongClickListener{
    private List <DetailView> detailViews;
    private Password password;
    private OnItemClickListener onItemClickListener = null;

    @Override
    public boolean onLongClick(View view) {
        if (onItemClickListener != null) {
            onItemClickListener.onLongClick(view,(int)view.getTag());
        }
        return true;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onLongClick(View view, int position);
    }
    @Override
    public void onClick(View view) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取position
            onItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        View textView;
        TextView displayName;
        TextView displayText;
        public ViewHolder(View view) {
            super(view);
            textView = view;
            displayName = (TextView) view.findViewById(R.id.display_name);
            displayText = (TextView) view.findViewById(R.id.display_text);
        }
    }

    public DetailAdpater(List<DetailView> detailViews, Password password) {
        this.detailViews = detailViews;
        this.password = password;
    }

    @Override
    public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        View view = LayoutInflater.from( parent.getContext()) .inflate( R.layout.detail_view, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.textView.setOnClickListener(this);
        holder.textView.setOnLongClickListener(this);
        return holder;
    }
    @Override
    public void onBindViewHolder( ViewHolder holder, int position) {
        DetailView d = detailViews.get(position);
        holder.displayName.setText( d.getDisplayName());
        holder.displayText.setText(d.getText());
        holder.itemView.setTag(position);
    }
    @Override
    public int getItemCount() {
        if(detailViews == null){
            return 0;
        }
        return detailViews.size();
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public Password getPassword() {
        return password;
    }
}
